package com.x9space.framework.config;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import com.x9space.common.config.X9SpaceConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.security.concurrent.DelegatingSecurityContextExecutorService;

import javax.annotation.Resource;

@Configuration
public class SpringAsyncConfig extends AsyncConfigurerSupport {

    @Resource
    private X9SpaceConfig x9SpaceConfig;

    /**
     * 异步执行需要使用权限框架自带的包装线程池 保证权限信息的传递
     */
    @Override
    public Executor getAsyncExecutor() {
        return new DelegatingSecurityContextExecutorService(
                Executors.newFixedThreadPool(x9SpaceConfig.getSpringAsyncExecutorNumberOfThreads())
        );
    }
}
